package com.bjc.base.utils;

import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.springframework.util.Base64Utils;

/**
 * 编码工具类 1.将byte[]转为各种进制的字符串 2.base 64 encode 3.base 64 decode 4.获取byte[]的md5值
 * 5.获取字符串md5值 6.结合base64实现md5加密 7.AES加密 8.AES加密为base 64 code 9.AES解密 10.将base
 * 64 code AES解密
 * 
 * @author uikoo9
 * @version 0.0.7.20140601
 */
public class QEncodeUtil {

	private static final String PASSWORD = "0123456789abcdef";
	private static final String PASSWORD2 = "!@#$bjcwxkqxxhIT";

	/**
	 * 加密
	 * 
	 * @param content
	 *            需要加密的内容
	 * @return
	 */
	public static String encrypt(String content) throws Exception {
		content = content + "|" + System.currentTimeMillis();
		byte[] encryptBytes = content.getBytes("utf-8");
		byte[] raw = PASSWORD.getBytes("utf-8");
		SecretKeySpec sks = new SecretKeySpec(raw, "AES");
		// 创建密码器
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		// 初始化
		cipher.init(Cipher.ENCRYPT_MODE, sks);
		byte[] result = cipher.doFinal(encryptBytes);
		// 加密
		return URLEncoder.encode(base64Encode(result), "UTF-8"); 
	}

	/**
	 * 解密
	 * 
	 * @param content
	 *            待解密内容
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String content) throws Exception {
		byte[] encryptBytes = base64Decode(content);
		byte[] raw = PASSWORD.getBytes("utf-8");
		SecretKeySpec sks = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, sks);
		byte[] decryptBytes = cipher.doFinal(encryptBytes);
		String result = new String(decryptBytes);
		return result.split("\\|")[0];
	}

	/**
	 * 考勤地址加密
	 * 
	 * @param content
	 *            需要加密的内容
	 * @return
	 */
	public static String encryptAtt(String content) throws Exception {
		content = content + "|" + System.currentTimeMillis();
		byte[] encryptBytes = content.getBytes("utf-8");
		byte[] raw = PASSWORD2.getBytes("utf-8");
		SecretKeySpec sks = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, sks);
		byte[] result = cipher.doFinal(encryptBytes);
		return URLEncoder.encode(base64Encode(result), "UTF-8");
	}

	/**
	 * 考勤地址解密
	 * 
	 * @param content
	 *            需要加密的内容
	 * @return
	 */
	public static String decryptExcep(String content) throws Exception {
		content = URLDecoder.decode(content, "UTF-8");
		byte[] encryptBytes = base64Decode(content);
		byte[] raw = PASSWORD2.getBytes("UTF8");
		SecretKeySpec sks = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, sks);
		byte[] decryptBytes = cipher.doFinal(encryptBytes);
		String result = new String(decryptBytes, "utf-8");
		return result.split("\\|")[0];
	}

	public static byte[] base64Decode(String bs) throws Exception {
		return Base64Utils.decodeFromString(bs);
	}

	public static String base64Encode(byte[] bytes) {
		return Base64Utils.encodeToString(bytes);
	}
}
